/**
 * Created with JetBrains PhpStorm.
 * User: Ari
 * Date: 8/1/13
 * Time: 8:40 PM
 * To change this template use File | Settings | File Templates.
 */
(function(){
    var EVENTS = 'hide show highlight append prepend html';

    var eventHandler = function(e) {
        var CurrentTarget = jQuery(e.currentTarget);
        var Target = jQuery(e.target);
        var type = e.type;
        var eArgs = arguments;
        var args = Array.prototype.slice.call(eArgs, 1);

        switch(type) {
//            case 'click':
//            case 'toggle':
//            case 'open':
//            case 'close':
//                var Toggle = Target.children('.click-toggle:first');
//                if(Toggle.length === 0)
//                    Toggle = Target.parents().children('.click-toggle:first');
//
//                if(Toggle && (Target[0] == Toggle[0] || Target[0] == Toggle.parent()[0])) {
//                    var Siblings = Toggle
//                        .siblings(':not(.stay-open)');
//                    if(type === 'toggle' || type === 'click')
//                        type = Siblings.filter(':hidden').length === 0 ? 'close' : 'open';
//                    Siblings
//                        .filter(type === 'close' ? ':visible' : ':hidden')
//                        .each(function(i, child) {
//                            e.currentTarget = child;
//                            e.target = child;
//                            e.type = type === 'close' ? 'hide' : 'show';
//                            eventHandler.apply(eventHandler, eArgs);
//                        });
//                    e.stopPropagation();
//                    return;
//                } else {
//                    type = type === 'close' ? 'hide' : 'show';
//                    break;
//                }

            case 'append':
            case 'prepend':

                var Parents = Target.parents('.container-item:first');
                if(Parents.length === 1) {
                    Parents[type].apply(Parents, args);
                    e.stopPropagation();
                    return;
                }

                var Template = jQuery('<div class="container-item"></div>');
                Template.append.apply(Template, args);
                CurrentTarget[type].call(CurrentTarget, Template);
                e.stopPropagation();
                return;
        }

        switch(type) {
            case 'hide':
            case 'show':
                if(Target.is('table')) {
                    var Cells = Target
                        .find('tr td, tr th')
                        .add(Target);
//                    var StayOpen = Cells.filter('.stay-open');
//                    if(StayOpen.length === 0) {
//                        Cells = Cells.add(Target);
//                    } else {
//                        Cells = Cells.not(StayOpen);
//                    }
                    (type === 'hide')
                        ? Cells.fadeOut.apply(Cells, args)
                        : Cells.fadeIn.apply(Cells, args);
                } else {
                    (type === 'hide')
                        ? Target.slideUp.apply(Target, args)
                        : Target.slideDown.apply(Target, args);
                }
                e.stopPropagation();
                return;
        }
    };

    var ready = function(e) {
        jQuery('.user-ui-container')
            .each(function(i, container) {
                var Container = jQuery(container);
                if(!Container.data('ready')) {
                    jQuery('body').on('append prepend html', function(e) {
                        e.target = Container[0];
                        e.currentTarget = Container[0];
                        Container.trigger.apply(Container, arguments);
                    });
                    Container.on(EVENTS, eventHandler);
                    Container.data('ready', true);
                }
            });
    };

    jQuery(document).ready(function(e) {
        jQuery('body').on('ready', ready);
        ready(e);
    });
})();

